package com.yunxun.coinmessage.controller;

import com.yunxun.coinmessage.entity.Coin;
import com.yunxun.coinmessage.entity.Exchange;
import com.yunxun.coinmessage.entity.Program;
import com.yunxun.coinmessage.entity.Subscribe;
import com.yunxun.coinmessage.sqlvo.SubscribeVO;
import com.yunxun.coinmessage.templatedata.CoinTemplateData;
import com.yunxun.coinmessage.templatedata.SubscribeTemplateData;
import com.yunxun.coinmessage.service.CoinService;
import com.yunxun.coinmessage.service.ExchangeService;
import com.yunxun.coinmessage.service.ProgramService;
import com.yunxun.coinmessage.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SubscribeController {
    @Autowired
    SubscribeService subscribeService;
    @Autowired
    CoinService coinService;
    @Autowired
    ExchangeService exchangeService;
    @Autowired
    ProgramService programService;


    //两个方法暂时未优化，存在的原因是为了左侧导航条点击时跳转subscrib-list页面
    @RequestMapping("/querySubscribeByPage")
    public String queryAll(@RequestParam("start") Integer start,@RequestParam("limit") Integer limit, Model model){
        Integer total=subscribeService.queryTotal();
        int totalPage=(total+19)/20;
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        return "subscribe-list";
    }

    @RequestMapping("/querySubscribeByPage1")
    @ResponseBody
    public List<SubscribeTemplateData> queryAllSubscribe(@RequestParam("start") Integer start, @RequestParam("limit") Integer limit){

        List<SubscribeTemplateData> subscribeTemplateDataList =subscribeService.querySubscribeByPage(start,limit);
        Integer total=subscribeService.queryTotal();
        return subscribeTemplateDataList;
    }
    //为指定用户添加订阅
    @RequestMapping("/addSubscribeByUser")
    @ResponseBody
    public ModelAndView addSubscribeByUser(@Valid Subscribe subscribe){
        //判断是否有重复订阅
        int tag=subscribeService.queryBySubscribe(subscribe);
        if(tag>0){
            return new ModelAndView("redirect:/querySubscribeByUid?uid="+subscribe.getUid()+"&tag=1");
        }
        //判断是否存在于数据库，存在则更新，不存在则新增
        if(subscribeService.queryExistDatabase(subscribe)>0){
            subscribeService.updateSubscribeStatus(subscribe);
        }else {
            subscribeService.addSubscribe(subscribe);
        }

        return new ModelAndView("redirect:/querySubscribeByUid?uid="+subscribe.getUid()+"&tag=2");
    }
    @RequestMapping("/openAddSubscribe")
    public String openAddSubscribe(Integer uid,Model model){
        List<CoinTemplateData> coinTemplateDataList=new ArrayList<>();
        List<Coin> coinList=coinService.queryAll();
        List<Exchange> exchangeList=exchangeService.queryAll();
        List<Program> programList=programService.queryAll();
        for(Coin coin:coinList){
            CoinTemplateData coinTemplateData=new CoinTemplateData();
            coinTemplateData.setSelectCoin("("+coin.getCcode().toLowerCase()+")"+coin.getCname());
            coinTemplateData.setCid(coin.getCid());
            coinTemplateDataList.add(coinTemplateData);
        }
        model.addAttribute("coinTemplateDataList",coinTemplateDataList);
        model.addAttribute("exchangeList",exchangeList);
        model.addAttribute("programList",programList);
        model.addAttribute("uid",uid);
       return "addSubscribeByUser";
    }
    @RequestMapping("/querySubscribeByUid")
    public String querySubscribeByUid(Integer uid,Integer tag,Model model){
        List<SubscribeTemplateData> subscribeTemplateDataList =subscribeService.querySubscribeByUid(uid);
        if(tag==1){
            model.addAttribute("msg","已经订阅了该条目，请勿重复订阅");
        }
        if(tag==2){
            model.addAttribute("msg","新增订阅成功");
        }
        model.addAttribute("subscribeTemplateDataList", subscribeTemplateDataList);

        return "subscribe-add";
    }

    /**
     * 先去用户表中模糊查询找到uidList，然后再到订阅表中遍历uidList找到用户的所有订阅
     * @param uname
     * @param model
     * @return
     */
    @RequestMapping("/querySubscribeByUname")
    public String querySubscribeByUname(String uname, Model model){
        List<SubscribeTemplateData> subscribeTemplateDataList=subscribeService.querySubscribeByUidList(uname);
        int total=subscribeTemplateDataList.size();
        model.addAttribute("subscribeTemplateDataList",subscribeTemplateDataList);
        model.addAttribute("uname",uname);
        model.addAttribute("total",total);
        return "subscribeByUname";
    }
}
